<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\model;

use app\common\model\OrderGoods as OrderGoodsModel;
use app\common\library\helper;
use app\common\model\OrderSub as OrderSubModel;

/**
 * 订单商品模型
 * Class OrderGoods
 * @package app\api\model
 */
class OrderGoods extends OrderGoodsModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'image',
        'content',
        'store_id',
        'create_time',
    ];

    /**
     * 获取未评价的商品
     * @param int $orderId 订单ID
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getNotCommentGoodsList(int $orderId)
    {
        return (new static)->with(['image', 'refund'])
            ->where('order_id', '=', $orderId)
            ->where('is_comment', '=', 0)
            ->select();
    }

    public static function updateOrderInfo($order)
    {
        return (new static)->transaction(function () use ($order) {
            $reult = self::updateOrderGoods($order);
            if ($reult) {
                //更新订单子表
                return OrderSubModel::saveOrderSub($order);
            }

        });

    }

    /**
     * 更新订单商品信息
     * @param $order
     * @return void
     */
    public static function updateOrderGoods($order)
    {
        if (!isset($order['order_id'])) {
            return false;
        }
        $goodsList = (new static)->where('order_id', '=', $order['order_id'])->select()->toArray();
        if (empty($goodsList)) {
            return false;
        }
        $data                  = [];
        $total_deduction_value = 0;
        foreach ($goodsList as $goods) {
            $pay_cash        = '0.00';
            $deduction_value = '0.00';
            if ($order['dk_money'] > 0 && in_array($order['bdType'], ['5', '6', '7','10'])) {
                //pay_money 现金支付金额 pay_price支付总金额
                if ($order['bdType'] == '10' && $order['dk_money'] == $order['pay_price'] && $order['pay_money'] == '0.00') {//纯余额支付
                    $deduction_value = $goods['total_pay_price'];
                }else if ($order['bdType'] == '7' && $order['dk_money'] == $order['pay_price'] && $order['pay_money'] == '0.00') {//纯邦豆支付
                    $deduction_value = $goods['total_pay_price'];
                } else {
                    //$orderPayPrice = helper::bcsub($order['orderPayPrice'],$order['expressPrice']);//支付金额减去快递费,暂时没有快递费
                    //$deduction_value = helper::bcmul(helper::bcdiv($order['dk_money'], $order['pay_price'], 10), $goods['total_pay_price']);//现金+三券组合支付
                    $deduction_value = round((float)helper::bcmul(helper::bcdiv($order['dk_money'], $order['pay_price'], 10), $goods['total_pay_price'],10),2);//现金+三券组合支付
                    if ($deduction_value <= 0) {
                        $deduction_value = 0.01;
                    }
                    $pay_cash = helper::bcsub($goods['total_pay_price'], $deduction_value);
                }
                $bd_type = $order['bdType'];
            } else {
                $pay_cash = $goods['total_pay_price'];
                $bd_type  = 0;
            }
            $total_deduction_value = helper::bcadd($total_deduction_value, $deduction_value);
            $data[]                = [
                'order_goods_id'  => $goods['order_goods_id'],
                'total_pay_price' => $goods['total_pay_price'],
                'pay_cash'        => $pay_cash,
                'bd_type'         => $bd_type,
                'deduction_value' => $deduction_value,
            ];
        }

        if ($total_deduction_value != $order['dk_money']) {//订单金额误差处理,订单最大金额做处理
            $maxValue = max(array_column($data, 'total_pay_price'));
            $key      = array_search($maxValue, array_column($data, 'total_pay_price'));
            $diff_dk  = helper::bcsub($order['dk_money'], $total_deduction_value);

            $data[$key]['deduction_value'] = helper::bcadd($data[$key]['deduction_value'], $diff_dk);
            $data[$key]['pay_cash']        = helper::bcsub($data[$key]['pay_cash'], $diff_dk);
        }
        foreach ($data as $item) {
            (new static)->where('order_goods_id', $item['order_goods_id'])->update($item);
        }
        return true;
    }

}
